package saleMarket.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import saleMarket.domain.Communication;
import saleMarket.domain.Customer;
import saleMarket.service.CommunicationService;
import saleMarket.service.CustomerService;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 徐元坤 付饶
 * @version 1.0  12-12-2020
 */

@Slf4j
@WebServlet("/communication.ctl")
public class CommunicationController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String communication_json = JSONUtil.getJSON(request);
        //将JSON字串解析为Customer对象
        Communication communicationToAdd = JSON.parseObject(communication_json, Communication.class);

        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加Customer对象
        try {
            //分页获得满足条件的所有聊天记录
            Collection<Communication> communications = CommunicationService.getInstance()
                    .communicate(communicationToAdd);
            //第二个参数可以防止循环引用
            String communication_jsonStr = JSON.toJSONString(communications,
                    SerializerFeature.DisableCircularReferenceDetect);
          System.out.println(communication_jsonStr);
            message.put("message", communication_jsonStr);
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            message.put("message", "网络异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
        //响应message到前端
        response.getWriter().println(message);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int customer_id = Integer.parseInt(request.getParameter("customerId")) ;
        int shopOwner_id = Integer.parseInt(request.getParameter("shopOwnerId"));
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            CommunicationService.getInstance().startAndRefresh(customer_id,shopOwner_id);
            respMessage_jsonObj.put("message","开始聊天");
            response.getWriter().println(respMessage_jsonObj);
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            e.printStackTrace();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int customer_id = Integer.parseInt(request.getParameter("customerId")) ;
        int shopOwner_id = Integer.parseInt(request.getParameter("shopOwnerId"));
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //到数据库表中所有数据
            CommunicationService.getInstance().deleteCase(customer_id,shopOwner_id);
            respMessage_jsonObj.put("message", "交流结束");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }
}
